package com.itheima.tlias84.service.impl;

import com.itheima.tlias84.annotation.Log;
import com.itheima.tlias84.controller.po.InsertDeptParam;
import com.itheima.tlias84.controller.po.UpdateDeptParam;
import com.itheima.tlias84.dao.entity.Dept;
import com.itheima.tlias84.dao.mapper.DeptMapper;
import com.itheima.tlias84.service.DeptService;
import com.itheima.tlias84.service.dto.SelectAllDeptDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    DeptMapper deptMapper;

    @Override
    public List<SelectAllDeptDTO> selectAll() {
        return deptMapper.selectAll();
    }

    @Override
    public SelectAllDeptDTO selectOne(Integer id) {
        return deptMapper.selectOne(id);
    }

    @Override
    public int insertDept(InsertDeptParam param) {
        Dept dept = new Dept();
        dept.setName(param.getName());
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        return deptMapper.insertDept(dept);
    }

    @Override
    public int batchInsertDept(List<InsertDeptParam> list) {
        List<Dept> depts = list.stream().map(param -> {
            Dept dept = new Dept();
            dept.setName(param.getName());
            dept.setCreateTime(LocalDateTime.now());
            dept.setUpdateTime(LocalDateTime.now());
            return dept;
        }).toList();
        return deptMapper.batchInsert(depts);
    }

    @Override
    public int deleteDeptById(Integer id) {
        return deptMapper.deleteDept(id);
    }

    @Override
    public int batchDeleteDept(List<Integer> idList) {
        return deptMapper.batchDelete(idList);
    }

    @Override
    public int updateDept(UpdateDeptParam param) {
        String s = deptMapper.selectByName(param);
        if (s != null) {
            return -1;
        }else {
            Dept dept = new Dept();
            dept.setId(param.getId());
            dept.setName(param.getName());
            dept.setUpdateTime(LocalDateTime.now());
            return deptMapper.updateDept(dept);
        }
    }
}
